import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
class MyBlackingQueue{
    private int[] data = new int[1000];
    private int head = 0;
    private int tail = 0;
    private int size = 0;

    private Object locker = new Object();

    public void Produce(int value) throws InterruptedException {
        synchronized (locker) {
            if (size == data.length) {
                locker.wait();
            }
            data[tail] = value;
            tail++;
            if (tail >= data.length) {
                tail = 0;
            }
            size++;
            locker.notify();
        }
    }

    public Integer Custom() throws InterruptedException {
        synchronized (locker) {
            if (size == 0) {
                locker.wait();
            }
            int ret = data[head];
            head++;
            if (head >= data.length) {
                head = 0;
            }
            size--;
            locker.notify();
            return ret;
        }
    }
}
public class Produce {
    private static MyBlackingQueue queue = new MyBlackingQueue();

        public static void main(String[] args) {
            Thread customer = new Thread(() -> {
                int num = 0;
                while(true){
                    try {
                        System.out.println("生产了: " + num);
                        queue.Produce(num);
                        num++;
                        //Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            customer.start();

            Thread p = new Thread(() ->{
                while(true){
                    try {
                        int num = queue.Custom();
                        System.out.println("消费了: " + num);
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            p.start();
    }

}
